<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: User.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class MicroblogUser  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'microblog_user';
		
		$this->primary= $this->_primary='user_id';
		parent::__construct();
	}
	
	

	function getSame($arr) {	
		$ret=array();
		
		$where['user_name']=$arr['user_name'];
		
	    
		return parent::getSame($where); 	
	
	}
	
	function login($username,$password){

		/*global $arr_config_index,$arr_config;
		$arr_config_index++;
		if($arr_config_index>=count($arr_config)){
			$arr_config_index=0;
		}
		extract($arr_config[$arr_config_index]);*/
		$preloginurl = 'http://login.sina.com.cn/sso/prelogin.php?entry=miniblog&callback=sinaSSOController.preloginCallBack&user='.$username.'&client=ssologin.js(v1.3.12)';
		$loginurl = 'http://login.sina.com.cn/sso/login.php';
		
		
		$result = request($preloginurl);
		
		preg_match('/"servertime":([0-9]+)/', $result, $servertime);
		preg_match('/"nonce":"([a-zA-Z_0-9]+)"/', $result, $nonce);
		
		$servertime = $servertime[1];
		$nonce = $nonce[1];
		$password = sha1(sha1(sha1($password)).$servertime.$nonce);
		
		//*
		$loginparam = array(
			'service' => 'miniblog',
			'client' => 'ssologin.js(v1.3.12)',
			'entry' => 'miniblog',
			'encoding' => 'utf-8',
			'gateway' => '1',
			'savestate' => '0',
			'useticket' => '1',
			'username' => $username,
			'servertime' => $servertime,
			'nonce' => $nonce,
			'pwencode' => 'wsse',
			'password' => $password,
			'url' => 'http://microblog.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack&returntype=META',
			'returntype' => 'META',
			'ssosimplelogin' => '1',
			'setdomain' => '1',
		);
		//*/
		
		preg_match('/&#39;(.*)&#39;/', request($loginurl.'?client=ssologin.js(1.3.12)', 'post', $loginparam), $url);
		request($url[1], 'get', array(), 2);	
	}
	
	function handlePage($user_id=0){
		//echo 'http://Microblog.com/'.$user_id;exit;
		$select=$this->_db->select()->from('users')->where('users_id=?',$user_id);
		if(!$this->_db->query($select)->fetchAll()){
			$content=request('http://Microblog.com/'.$user_id,'get',array(),1);
			
	
			$doc=phpQuery::newDocumentHTML($content);
			
			
			$user_name='';
			$result_user_name=pq('#other_info div.name .lf');
			foreach($result_user_name as $v_user_name){
				$user_name=$v_user_name->nodeValue;
				break;
			}
	
			$fans=0;
			$result_fans=pq('#myfans');
			foreach($result_fans as $v_fans){
				$fans=$v_fans->nodeValue;
				break;
			}
	
			$mention_count=0;
			$result_mention_count=pq('#mblog');
			foreach($result_mention_count as $v_mention_count){
				$mention_count=$v_mention_count->nodeValue;
				break;
			}
			
			$row=array(		
				'users_id'=>$user_id,
				'user_name'=>$user_name,
				'followers_count'=>$fans,
				'mention_count'=>$mention_count,		
			);
			
			try{
				$this->insert($row);
			}catch(Exception $e){
				print_r($row);
			}
		}else{
			//sleep(2);
		}
		//echo $content;exit;
		return $row;
	}


}
